package hlhj.fhp.checkpoint.atys

import android.annotation.SuppressLint
import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.widget.RadioButton
import com.example.mymvp.log

import com.example.mymvp.mvp.BaseModule
import com.example.mymvp.mvp.BasePresenter
import com.example.mymvp.mvp.BaseView
import com.tencent.bugly.beta.Beta

import hlhj.fhp.checkpoint.BaseAty
import hlhj.fhp.checkpoint.Constant
import hlhj.fhp.checkpoint.R
import hlhj.fhp.checkpoint.adp.HomePagerAdp
import hlhj.fhp.checkpoint.fgms.HomeFgm
import hlhj.fhp.checkpoint.fgms.HomeOrderFgm
import hlhj.fhp.checkpoint.fgms.HomeSelfFgm
import hlhj.fhp.checkpoint.iview.ActiveView
import hlhj.fhp.checkpoint.utils.ToastUtils
import kotlinx.android.synthetic.main.atyhome.*

/**
 * Created by Never Fear   on 2018\11\6 0006.
 * Never More....
 */

class HomeAty : BaseAty<BaseView, BasePresenter<*, BaseView>>(), BaseView, ActiveView {
    override val contentId: Int
        get() = R.layout.atyhome


    private var pagerAdp:HomePagerAdp?=null
    private val fgmLists=ArrayList<Fragment>()
    private var isExit=false
    private var mHandler= @SuppressLint("HandlerLeak")
    object :Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            val result = msg!!.obj as Map<String, String>
            log("fhp","回调为${result.toString()}")
            val status = result["resultStatus"]
            ToastUtils.toast("支付成功",this@HomeAty)
            if (status.equals("9000")){
                Constant.isActive=true
            }else{

            }
        }
    }
    override fun initView() {

        fgmLists.add(HomeFgm())
        fgmLists.add(HomeOrderFgm())
        fgmLists.add(HomeSelfFgm())
        rgHome.isChecked=true

        pagerAdp= HomePagerAdp(supportFragmentManager,fgmLists)
        viewPager.adapter=pagerAdp
        viewPager.offscreenPageLimit=5
    }

    override fun onBackPressed() {
        if (!isExit){
            ToastUtils.toast("再按一次退出App",this)
            isExit=!isExit
            Thread(Runnable {
                Thread.sleep(2000)
                isExit=!isExit
            }).start()
        }else{
            super.onBackPressed()

//            System.exit(0)
        }
    }
    override fun setListener() {
        viewPager.addOnPageChangeListener(object :ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                val button = bottomBar.getChildAt(position) as RadioButton
                button.isChecked=true
            }
        })
        bottomBar.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.rgHome->{
                    viewPager.currentItem=0
                }
                R.id.rgOrder->{
                    if (!Constant.isActive){
                        ToastUtils.toast("前去激活用户",this)
                        val intent = Intent(this, PayAty::class.java)
                        intent.putExtra("id","")
                        intent.putExtra("money",Constant.Active_Money)
                        intent.putExtra("orderType",2)
                        startActivity(intent)

                        val button = bottomBar.getChildAt(0) as RadioButton
                        button.isChecked=true
                    }else{
                        viewPager.currentItem=1
                    }

                }
                R.id.rgSelf->{
                    if (!Constant.isActive){
                        ToastUtils.toast("前去激活用户",this)
                        val intent = Intent(this, PayAty::class.java)
                        intent.putExtra("id","")
                        intent.putExtra("money",Constant.Active_Money)
                        intent.putExtra("orderType",2)
                        startActivity(intent)

                        val button = bottomBar.getChildAt(0) as RadioButton
                        button.isChecked=true
                    }else{
                        viewPager.currentItem=2
                    }

                }

            }
        }
    }

    override fun logicStart() {

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun bindView(): BaseView {
        return this
    }

    override fun createPresenter(): BasePresenter<*, BaseView> {
        return object : BasePresenter<BaseModule, BaseView>() {
            override fun createModule() {

            }
        }
    }

    override fun setActiveOrderSign(beanStr: String) {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        log("fhp","ATY里${resultCode}")
        if (resultCode!= Activity.RESULT_OK)return
        when(requestCode){
            11->{//从添加那里过来
                val homeFgm = fgmLists[0] as HomeFgm
                homeFgm.refreshPage()
            }

        }

    }
}
